﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace FlickrWP7.Core
{
    public class Favorites
    {
        private HelperDelegate helperDelegateInstance;

        public string LastFavoritesResult { get; set; }

        public void AddFavorite(string apiKey, string authToken, string signature, string photoID, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;
            string URL = string.Format(
                "http://www.flickr.com/services/rest/?method=flickr.favorites.add&api_key={0}&api_sig={1}&auth_token={2}&photo_id={3}&format=json",
                apiKey,signature,authToken,photoID);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";

            request.BeginGetResponse(asyncCallback =>
            {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncCallback);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    LastFavoritesResult = reader.ReadToEnd();
                }
                helperDelegate();
            }, null);
        }

        public void RemoveFavorite(string apiKey, string authToken, string signature, string photoID, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;
            string URL = string.Format(
                "http://www.flickr.com/services/rest/?method=flickr.favorites.remove&api_key={0}&api_sig={1}&auth_token={2}&photo_id={3}&format=json",
                apiKey, signature, authToken, photoID);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";

            request.BeginGetResponse(asyncCallback =>
            {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncCallback);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    LastFavoritesResult = reader.ReadToEnd();
                }
                helperDelegate();
            }, null);
        }

        public void GetList(string apiKey, string authToken, string signature, string[] extras,
            HelperDelegate helperDelegate, string userID = "",  string minFaveDate = "", string maxFaveDate = "", string page = "",
            string perPage = "")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.favorites.getList&api_key={0}&api_sig={1}&auth_token={2}&format=json",
                    apiKey, signature, authToken);

            if (extras != null)
            {
                string toAdd = "&extras=";
                for (int i =0; i< extras.Length; i++)
                {
                    if (i != extras.Length - 1)
                        toAdd += extras[i] + ",";
                    else
                        toAdd += extras[i];
                }
                URL += toAdd;
            }

            if (userID != "")
                URL += "&user_id=" + userID;
            if (minFaveDate != "")
                URL += "&min_fave_date=" + minFaveDate;
            if (maxFaveDate != "")
                URL += "&max_fave_date=" + maxFaveDate;
            if (page != "")
                URL += "&page=" + page;
            if (perPage != "")
                URL += "&per_page=" + perPage;

            client.DownloadStringAsync(new Uri(URL));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            LastFavoritesResult = e.Result;
            helperDelegateInstance();
        }
    }
}
